package UILayer;
import CtrLayer.*;
import ModLayer.*;
import java.util.Scanner;

/**
 * Write a description of class OfficeUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class OfficeUI
{
    private PersonCtr personCtr;
    private TimeAndCheck timeAndCheck;

    public OfficeUI()
    {
        personCtr = personCtr.getInstance();
        timeAndCheck = timeAndCheck.getInstance();
    }

    public void start()
    {
        boolean finished = false; 
        while(!finished)
        {
            printMenu();
            switch(reader(3))
            {
                case "0": finished = true; break;
                case "1": employeeManagement(); break;
                case "2": companyInfoManagement(); break;
                case "3": discountManagement(); break;
            }
        }
    }

    private void printMenu()
    { 
        System.out.println("\f*** Office menu ***");
        System.out.println("\nWhat would you like to do?");
        System.out.println("(0) Back to the main menu.");
        System.out.println("(1) Employee management.");
        System.out.println("(2) Company management.");
        System.out.println("(3) Discount management.");
        System.out.println("Make your choice: ");
    }

    private String reader2()
    { 
        Scanner command = new Scanner(System.in); 
        System.out.print(">> "); 
        String c = command.nextLine(); 
        return c;
    }

    private String reader(int limitIndex)
    { 
        final String[] validNumbers = {"0","1","2","3","4","5","6","7","8","9"}; 
        Scanner command = new Scanner(System.in); 
        String c = null; 
        boolean finished = false;
        while(!finished)
        { 
            System.out.print("\n> "); 
            c = command.nextLine();
            for(int i=0; i<=limitIndex; i++)
            {
                if(validNumbers[i].equals(c)) 
                    finished = true;
            }
            if(finished == false) 
            {
                System.out.print("\nTry once again");
                c = null;
            }
        }
        return c; 
    }

    private void showAllEmployeesForEmployeeManagement()
    {
        System.out.println("\f*** Show all employees ***");
        int numbering = 1;
        for(PersonInformations personObj: personCtr.getPeopleFromSpecificClass("Employee"))
        {
            Employee eObj = (Employee) personObj;
            System.out.println("\nEmployee "+numbering+":");
            System.out.println("Name of employee: "+personObj.getName());
            System.out.println("ID of employee: "+eObj.getID());
            System.out.println("Profession of employee: "+eObj.getJob());
            numbering++;
        }
    }

    private void getInfoEmployeeForEmployeeManagement(String id)
    {
        System.out.println("\nName of employee: "+personCtr.getInfoEmployee(id, "2"));
        System.out.println("Address of employee: "+personCtr.getInfoEmployee(id, "0"));
        System.out.println("Phone number of employee: "+personCtr.getInfoEmployee(id, "3"));
        System.out.println("Email of employee: "+personCtr.getInfoEmployee(id, "1"));
        System.out.println("Number of bank account of employee: "+personCtr.getInfoEmployee(id, "4"));
        System.out.println("ID of employee: "+personCtr.getInfoEmployee(id, "5"));
        System.out.println("Salary of employee: "+personCtr.getInfoEmployee(id, "6"));
        System.out.println("Profession of employee: "+personCtr.getInfoEmployee(id, "7"));
        System.out.println("Turnover of employee: "+personCtr.getInfoEmployee(id, "8"));
    }

    private void getInfoCompanyForEmployeeManagement()
    {
        System.out.println("\nName of company: "+personCtr.getInfoCompany("2"));
        System.out.println("Address of company: "+personCtr.getInfoCompany("0"));
        System.out.println("Phone number of company: "+personCtr.getInfoCompany("3"));
        System.out.println("Email of company: "+personCtr.getInfoCompany("1"));
        System.out.println("Fax number of company: "+personCtr.getInfoCompany("5"));
        System.out.println("Bank account number of company: "+personCtr.getInfoCompany("4"));
    }

    private void employeeManagement()
    {
        boolean finished = false;
        System.out.println("\f"); 
        while(!finished)
        {
            System.out.println("\n*** Employee management ***");
            System.out.println("\nWhat would you like to do?");
            System.out.println("(0) Back to the employee menu.");
            System.out.println("(1) Show all employees.");
            System.out.println("(2) Add employee.");
            System.out.println("(3) Delete employee.");
            System.out.println("(4) Update employee.");
            System.out.println("(5) Show all informations about employee.");
            System.out.println("Make your choice: ");
            switch(reader(5)) 
            { 
                case "0": finished = true; break;
                case "1": showAllEmployeesForEmployeeManagement(); break;

                case "2":
                System.out.println("\f*** Add employee ***");
                System.out.println("Type in name of employee");
                String name = reader2();
                System.out.println("\nType in email of employee");
                String email = reader2();
                System.out.println("\nType in address of employee");
                String address = reader2();
                System.out.println("\nType in phone number of employee");
                String phoneNo = reader2();
                System.out.println("\nType in profession of employee");
                String job = reader2();
                System.out.println("\nType in number of bank account of employee");
                String bankAccountNo = reader2();
                System.out.println("\nType in ID of employee");
                String id = reader2();
                System.out.println("\nType in salary of employee");
                String salary = reader2();
                if(timeAndCheck.checkStringToInt(salary)==null)
                    salary = null;
                if(salary!=null)
                {
                    personCtr.createEmployee(name, email, address, phoneNo, job, bankAccountNo, id, salary);
                    System.out.println("\fEmployee was created.");
                }
                else
                {
                    System.out.println("\fIncorrect format of salary, try once again.");
                }
                break;

                case "3":
                System.out.println("\f*** Delete employee ***");
                System.out.println("Type in employee ID: ");
                String deleteEmployee = reader2();
                if(personCtr.searchPerson(deleteEmployee, "office")!=null)
                {
                    personCtr.deletePerson(deleteEmployee, "office");
                    System.out.println("\fEmployee was deleted.");
                }
                else
                    System.out.println("\fEmployee not found, try once again.");
                break;

                case "4":
                System.out.println("\f*** Update employee ***");
                System.out.println("Type in employee ID: ");
                String employeeID = reader2();
                boolean finished2 = false;
                if(personCtr.searchPerson(employeeID, "office")!=null)
                {
                    while(!finished2)
                    {
                        System.out.println("\f*** Update employee "+employeeID+" ***");
                        getInfoEmployeeForEmployeeManagement(employeeID);

                        System.out.println("\nWhat would you like to update?");
                        System.out.println("(0) Back to the employee management.");
                        System.out.println("(1) Address.");
                        System.out.println("(2) Email.");
                        System.out.println("(3) Name.");
                        System.out.println("(4) Phone number.");
                        System.out.println("(5) Number of bank account.");
                        System.out.println("(6) ID.");
                        System.out.println("(7) Salary.");
                        System.out.println("(8) Occupation.");
                        System.out.println("Make your choice: ");
                        switch(reader(8)) 
                        {
                            case "0": finished2 = true; break; 
                            case "1":
                            System.out.println("\fType in new address of employee:");
                            String addresss = reader2();
                            personCtr.updateEmployee(employeeID, addresss, "0");
                            break;

                            case "2":
                            System.out.println("\fType in new email of employee:");
                            String emaill = reader2();
                            personCtr.updateEmployee(employeeID, emaill, "1");
                            break;

                            case "3":
                            System.out.println("\fType in new name of employee:");
                            String namee = reader2();
                            personCtr.updateEmployee(employeeID, namee, "2");
                            break;

                            case "4":
                            System.out.println("\fType in new phone number of employee:");
                            String phoneNumber = reader2();
                            personCtr.updateEmployee(employeeID, phoneNumber, "3");
                            break;

                            case "5":
                            System.out.println("\fType in new number of bank account of employee:");
                            String bankAccountNoo = reader2();
                            personCtr.updateEmployee(employeeID, bankAccountNoo, "4");
                            break;

                            case "6":
                            System.out.println("\fType in new ID of employee:");
                            String idd = reader2();
                            if(timeAndCheck.checkStringToInt(idd)!=null)
                                personCtr.updateEmployee(employeeID, idd, "5");
                            else
                                System.out.println("\fIncorrect format.");
                            break;

                            case "7":
                            System.out.println("\fType in new salary of employee:");
                            String salaryy = reader2();
                            personCtr.updateEmployee(employeeID, salaryy, "6");
                            break;

                            case "8":
                            System.out.println("\fType in new profession of employee:");
                            String jobb = reader2();
                            personCtr.updateEmployee(employeeID, jobb, "7");
                            break;
                        }
                    }
                }
                else
                    System.out.println("\fEmployee not found, try once again.");
                break;

                case "5":
                System.out.println("\fType in employee ID: ");
                String employeeIDd = reader2();
                System.out.println("\f*** Show all informations ***");
                System.out.println("Employee ID: "+employeeIDd);
                getInfoEmployeeForEmployeeManagement(employeeIDd);
                break;
            }
        }
    }

    public void companyInfoManagement()
    {
        boolean finished = false;
        System.out.println("\f"); 
        while(!finished)
        {
            System.out.println("\n*** Company information management ***");
            System.out.println("\nWhat would you like to do?");
            System.out.println("(0) Back to the employee menu.");
            System.out.println("(1) Update information.");
            System.out.println("(2) Show all informations about company.");
            System.out.println("Make your choice: ");
            switch(reader(2)) 
            { 
                case "0": finished = true; break;
                
                case "1":
                System.out.println("\f");
                boolean finished2 = false;
                while(!finished2)
                {
                    System.out.println("\n*** Update company ***");
                    getInfoCompanyForEmployeeManagement();

                    System.out.println("\nWhat would you like to update?");
                    System.out.println("(0) Back to the employee management.");
                    System.out.println("(1) Address.");
                    System.out.println("(2) Email.");
                    System.out.println("(3) Name.");
                    System.out.println("(4) Phone number.");
                    System.out.println("(5) Number of bank account.");
                    System.out.println("(6) Fax number.");
                    System.out.println("Make your choice: ");
                    switch(reader(6)) 
                    {
                        case "0": finished2 = true; break; 
                        case "1":
                        System.out.println("\fType in new address of company:");
                        String addresss = reader2();
                        personCtr.updateCompany(addresss, "0");
                        break;

                        case "2":
                        System.out.println("\fType in new email of company:");
                        String emaill = reader2();
                        personCtr.updateCompany(emaill, "1");
                        break;

                        case "3":
                        System.out.println("\fType in new name of company:");
                        String namee = reader2();
                        personCtr.updateCompany(namee, "2");
                        break;

                        case "4":
                        System.out.println("\fType in new phone number of company:");
                        String phoneNumberr = reader2();
                        personCtr.updateCompany(phoneNumberr, "3");
                        break;

                        case "5":
                        System.out.println("\fType in new number of bank account of company:");
                        String bankAccountNoo = reader2();
                        personCtr.updateCompany(bankAccountNoo, "4");
                        break;

                        case "6":
                        System.out.println("\fType in new fax number of company:");
                        String faxNo = reader2();
                        personCtr.updateCompany(faxNo, "5");
                        break;
                    }
                }
                break;

                case "2":
                System.out.println("\f*** Show all informations ***");
                getInfoCompanyForEmployeeManagement();
            }
        }
    }

    private void discountManagement()
    {
        boolean finished = false;
        System.out.println("\f");
        while(!finished)
        {
            System.out.println("\n***Discount management ***");
            System.out.println("\nWhat would you like to do?");
            System.out.println("(0) Back to the employee menu.");
            System.out.println("(1) Show fixed discount.");
            System.out.println("(2) Change fixed discount.");
            System.out.println("Make your choice: ");
            switch(reader(2))
            {
                case "0": finished = true; break;

                case "1":
                System.out.println("\f*** Show fixed discount ***");
                System.out.println("\nValue of discount - "+personCtr.getInfoCompany("6")+"%");
                break;

                case "2":
                System.out.println("\f*** Change fixed discount ***");
                System.out.println("Type in new amount:");
                String fixedDiscount = reader2();
                if(timeAndCheck.checkStringToDouble(fixedDiscount)!=0)
                {
                    System.out.println("\f");
                    if(personCtr.updateCompany(fixedDiscount, "6")==true)
                        System.out.println("New fixed discount was set to - "+fixedDiscount+"%.");
                    else
                        System.out.println("Discount can not exceed 25%.");
                }
                else
                    System.out.println("\fIncorrect format.");
                break;
            }
        }
    }
}